﻿#region Copyright (c) 2008 plexBB Team
/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Collections;
using System.Web.UI.WebControls;

namespace PlexBB
{
    public static class ControlExtensions
    {
        /// <summary>
        /// Strongly-typed implementation of Control.FindControl method. Throws exception if control was not found.
        /// </summary>
        /// <typeparam name="T">Type of the control to be found.</typeparam>
        /// <param name="control">Target control.</param>
        /// <param name="id">The identifier of the control to be found.</param>
        /// <returns>Server control in the curernt naming container with the specified id parameter.</returns>
        public static T Find<T>(this Control control, string id) where T : class
        {
            return Find<T>(control, id, false);
        }

        public static T Find<T>(this Control control, string id, bool require) where T : class
        {
            Control c = control.FindControl(id);
            T t = c as T;
            if (require && c == null)
            {
                throw new InvalidOperationException(string.Format("Control {0} was not found.", id));
            }
            if (c != null && t == null)
            {
                throw new InvalidOperationException(string.Format("Expected {0} to be control of type {1}. Instead, it is of type {2}.",
                    id, typeof(T).FullName, c.GetType().FullName));
            }
            return t;
        }

        public static bool TrySelectValue(this ListControl control, string value)
        {
            ListItem item = control.Items.FindByValue(value);
            if (item != null)
            {
                control.SelectedIndex = -1;
                item.Selected = true;
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void AppendAttribute(this IAttributeAccessor control, string key, string value)
        {
            string oldValue = control.GetAttribute(key);
            control.SetAttribute(key, string.IsNullOrEmpty(oldValue) ? value : oldValue + ' ' + value);
        }

        //public static void MinimizeID(this Control control)
        //{
        //    IDictionary contextItems = HttpContext.Current.Items;
        //    int minimizedIDs = (int)(contextItems["PlexBB_minimizedIDs"] ?? 0);
        //    string id = Util.ConvertToMaxBase(minimizedIDs);
        //    contextItems["PlexBB_minimizedIDs"] = minimizedIDs + 1;
        //    control.ID = char.IsDigit(id, 0) ? "_"+ id : id;
        //}
    }
}
